<template>
  <el-container id="hero" direction="horizontal">
    <img :src="BASE_URL + goods.goodsImage" class="goods-image" alt="商品图片" :title="goods.goodsName">
    <el-container class="goods-desc" direction="vertical">
      <el-link icon="el-icon-back" type="primary" @click='goBack'>返回上级</el-link>
      <h2>{{goods.goodsName}}</h2>
      <div class="store-name">{{seller.storeName}}</div>
      <div class="element"><el-rate v-model="goods.star" disabled show-score text-color="#ff9900" score-template="{value}"/></div>
      <div class="element"><el-radio v-model="orderDetail.detailId" :label="item.detailId" v-for="(item, index) in detailList" :key="index" border>{{ item.detailName }}</el-radio></div>
      <div class="element">
        <el-input-number v-model="orderDetail.detailNum" controls-position="right" @change="handleChange" :min="1"/>
        <span style=" display: inline-block; width: 50px; height: 20px"></span>
        <el-button type="primary" round="round" icon="el-icon-present" @click="submitOrder()">立即购买</el-button>
        <el-button type="primary" round="round" icon="el-icon-shopping-cart-1">购物车</el-button>
      </div>
      <el-tabs type="border-card" style="padding-bottom: 0">
        <el-tab-pane label="商品描述"><Description :description="goods.memo"/></el-tab-pane>
        <el-tab-pane label="商品评论"><Comment :comments="comment"/></el-tab-pane>
      </el-tabs>
    </el-container>
  </el-container>
</template>

<script>

import { getGoods } from "@/api/shopping/goods";
import { addOrder } from "@/api/shopping/order";
import Comment from "@/components/Forestage/chocolate/Comment.vue";
import { comment } from "@/api/mockdata";
import Description from "@/components/Forestage/chocolate/Description.vue";

export default {
  name: 'Goods',
  computed: {
    comment() {
      return comment
    }
  },
  components: {
    Description,
    Comment
  },
  created() {
    this.goodsId = this.$route.params.goodsId;
    getGoods(this.goodsId).then(response => {
      this.goods = response.data;
      this.detailList = response.data.detailList;
      this.seller = response.data.seller;
      this.total = response.data.detailList.length;
      this.star = response.data.goods.star;
    });
  },
  mounted() {

  },
  data() {
    return {
      BASE_URL: "https://0w0-0v0.s3.ap-northeast-2.amazonaws.com/images/goods/",
      goodsId: null,
      goods: null,
      seller: null,
      detailList: null,
      total: null,
      star: null,
      orderDetail: {
        detailId: null,
        detailNum: 1,
      },
      isListening: false
    }
  },
  methods: {
    handleChange(value) {
    },
    submitOrder() {
      if (this.verify()) {
        addOrder(this.orderDetail).then(response => {
          let orderId = response.data;
          this.$router.push({name: 'orderDetail', params: {'orderId': orderId}});
        })
      } else {
        this.$notify({title: '警告', message: '商品规格未选择', type: 'warning', duration: 1500});
      }
    },
    verify() {
      return this.orderDetail.detailId != null && this.orderDetail.detailNum > 0;
    },
    startListening() {
      // 开始监听鼠标滚轮事件
      this.isListening = true;
      document.addEventListener('wheel', this.handleWheel);
    },
    stopListening() {
      // 停止监听鼠标滚轮事件
      this.isListening = false;
      document.removeEventListener('wheel', this.handleWheel);
    },
    handleWheel(event) {
      // 阻止默认的滚动行为
      // event.preventDefault();

      // 根据滚轮方向增加或减少数值
      this.orderDetail.detailNum -= (event.deltaY/100);

    },
    goBack() {
      this.$router.go(-1);
    }
  }
}
</script>

<style scoped lang="scss">
#id {
  flex: 1;
}

.goods-image {
  width: 660px;
  object-fit: cover;
  mask-image: -webkit-gradient(linear, left 10%, right 20%, from(black), to(transparent));
}

.element {
  padding: 10px 0;
}
.el-link {
  width: fit-content;
}
.store-name {
  color: gray;
}

.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}

.el-tab-pane {
  height: 280px;
  overflow: scroll;
}
</style>
